import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { Role } from 'src/global-type';

@Schema({ timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' } })
export class SystemUser {
  @Prop({ type: String, required: true, index: { unique: true }, trim: true })
  account: string;

  @Prop({ type: String, required: true, trim: true })
  password: string;

  @Prop({ type: String, required: true, trim: true, index: true })
  userName: string;

  @Prop({
    type: String,
    default:
      'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
  })
  avatar: string;

  @Prop({ type: String, default: Role.STAFF })
  role: Role;

  @Prop({ type: String, default: '' })
  phoneNumber: string;

  @Prop({ type: String, default: '' })
  email: string;

  @Prop({ type: String, default: '' })
  ownerId: string; // 普通员工ownerId值为企业管理员账号；系统管理员，企业管理员账号和ownerId同值；

  @Prop({ type: String, default: '' })
  ownerName: string;

  @Prop({ type: Date })
  lastLogin: Date;

  @Prop({ type: Number })
  loginTimes: number;

  @Prop({ type: String })
  auth: string;

  @Prop({ type: Date })
  createTime: Date;

  @Prop({ type: Date })
  updateTime: Date;
}

export type SystemUserDocument = SystemUser & Document;

export const SystemUserSchema = SchemaFactory.createForClass(SystemUser);
SystemUserSchema.index({ createTime: -1 });
SystemUserSchema.index({ lastLogin: -1 });
